// pages/ynamic/index.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
    trans:false,
		id:0,
		trends: {
			img_url: '../images/logo.png',
			tile: '',
			tile_two: '',
			publish: '',
			time: '',
			ti_top: '',
			video_prodet: '',
			img_bg: '',
			number: '0',
			itars_trfalse: false,
			pdf_file:'',
			pdf_title:'',
			size:'',
		},
		// 海报数据
		userInfo: {
			avatarUrl: '/pages/images/logo.png', //logo
			qrcode: '/pages/images/tym.png',//太阳码
			lovideo: '/pages/images/bar.png', // 视频背景
			// playdata: '/pages/images/play.png', 
			nickName: '上海蜜度科技股份有限公司', //标题
			ntishi: '长按二维码进入小程序',
			mobile: '蜜度校对通入选《2023年度上海市第一...蜜度校对通入选《2023年度上海市第一...蜜度校对通入选《2023年度上海市第一...', //内容
		},
		poste_material:{},
		generated: false, //加载
		showShare: false,
		options: [
			{ name: '分享微信好友', icon: 'wechat', openType: 'share' },
			{ name: '分享朋友圈', icon: 'wechat-moments' },
		],
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	//弹出海报
	poster_up: function () {

	},
	//素材加载
//     poster_fusion: function () {
//         var that = this;
//         that.setData({
//             poste_material: {
//                 width: '750rpx',
//                 height: '1333rpx',
//                 background: '#fafafa',
//                 views: [{
//                         type: 'image',
//                         url: this.data.rebate.rebate_img, //背景
//                         css: {
//                             top: `0rpx`,
//                             left: `0rpx`,
//                             width: '750rpx',
//                             height: '1333rpx',
//                             mode: 'scaleToFill',
//                         }
//                     },
//                     {
//                         type: 'text',
//                         text: this.data.userInfo.phone, //电话
//                         css: {
//                             align: 'center',
//                             width: '750rpx',
//                             left: '640rpx',
//                             textAlign: 'center',
//                             fontSize: '28rpx',
//                             fontWeight: 'bold',
//                             top: '70rpx',
//                             color: '#f40000'
//                         },
//                     },
//                     {
//                         type: 'image',
//                         url: this.data.rebate.code_img, //自己的二维码
//                         css: {
//                             bottom: `65rpx`,
//                             right: `65rpx`,
//                             width: '190rpx',
//                             height: '190rpx',
//                             mode: 'scaleToFill',
//                         }
//                     },
//                 ],
//             },
//         });
//     },
	//素材加载
	poster_fusion: function () {
		var that = this;
		console.log('that.data.userInfo.avatarUrl');
		console.log(that.data.userInfo.avatarUrl);
		that.setData({
			poste_material: {
				width: '676rpx',
				height: '853rpx',
				background: '#fff',
				views: [
				// 	{
				// 	type: 'image',
				// 	url: '/images/poster/user_bg.jpg', //背景
				// 	css: {
				// 		top: `0rpx`,
				// 		left: `0rpx`,
				// 		width: '750rpx',
				// 		height: '1221rpx',
				// 		mode: 'scaleToFill',
				// 	}
				// },
				{
					type: 'image',
					url: that.data.userInfo.avatarUrl, //头像
					css: {
						top: '54rpx',
						left: '55rpx',
						borderRadius: '177rpx',
						borderWidth: '3rpx',
						borderColor: '#ffffff',
						width: '79rpx',
						height: '79rpx',
					},
				},
				{
					type: 'text',
					text: that.data.userInfo.nickName, //昵称
					css: {
						align: 'center',
						width: '750rpx',
						left: '356rpx',
						textAlign: 'center',
						fontSize: '32rpx',
						fontWeight: 'bold',
						top: '78rpx',
						color: '#050505'
					},
				},
				{
					type: 'image',
					url: that.data.userInfo.qrcode, //自己的二维码
					css: {
						bottom: `55rpx`,
						right: `55rpx`,
						width: '98rpx',
						height: '98rpx',
						mode: 'scaleToFill',
					}
				},
				{
					type: 'text',
					text: that.data.userInfo.ntishi, //提示
					css: {
						bottom: `84rpx`,
						align: 'center',
						width: '130rpx',
						right: '250rpx',
						textAlign: 'center',
						fontSize: '24rpx',
						fontWeight: '400',
						color: '#AEAEB9'
					},
				},
				{
					type: 'text',
					text: that.data.userInfo.mobile, //内容
					css: {
						bottom: `261rpx`,
						maxLines: '1',
						left: `55rpx`,
						width: '500rpx',
						fontSize: '30rpx',
						fontWeight: '400',
						color: '#040F29'
					},
				},
				{
					type: 'image',
					url: that.data.userInfo.lovideo, //视频图片
					css: {
						top: '183rpx',
						left: '55rpx',
						borderRadius: '20rpx',
						width: '566rpx',
						height: '319rpx',
						mode: 'scaleToFill',
					},
				},
				{
					type: 'image',
					url: that.data.userInfo.playdata, //头像
					css: {
						top: '293rpx',
						left: '288rpx',
						borderRadius: '50rpx',
						width: '100rpx',
						height: '100rpx',
						mode: 'scaleToFill',
					},
				},
				],
			},
		});
	},
	//长海报成功
	async CImgOK(e) {
		var that = this;
		await wx.showShareImageMenu({ //可以将图片发送给朋友、收藏或下载
			path: e.detail.path,
			success: function (res) {
				console.log('成功')
				console.log(res)
			},
			fail: (msg) => {
				console.log('失败')
				console.log(msg)
			},
			complete: function () {
				console.log('成功或失败都执行')
			}
		})
		setTimeout(() => {
			that.setData({
				generated: false
			});
		}, 500)
	},
	//长海报失败
	CImgErr(e) {
		var that = this;
		//延时为了看效果
		setTimeout(() => {
			that.setData({
				generated: false
			});
			wx.showModal({
				title: '系统提示',
				content: '生成海报时发生错误,错误信息：' + e.detail.error,
				showCancel: false,
				confirmText: '上报错误',
				success(res) {
					if (res.confirm) {
						console.log('用户点击确定并上传错误信息')
					}
				}
			})
		}, 500)
	},
	url_pdfar(){
		let url = this.data.trends.pdf_file;
		wx.showLoading({
			title: '打开中...',
			mask:true,
		})
		wx.downloadFile({
			url: url,
			success: (res) => {
				if (res.statusCode === 200) {
					wx.getFileInfo({
						filePath: res.tempFilePath,
						success: (fileRes) => {
							var fileSizeBytes = fileRes.size;
							var fileSizeMB = (fileSizeBytes / 1024 / 1024).toFixed(2);
							var fileName = res.tempFilePath.substring(res.tempFilePath.lastIndexOf('/') + 1);
							wx.openDocument({
								filePath: res.tempFilePath,
								success: (res) => {
									console.log(res);
								}
							})
						},
						fail: (fileRes) => {
							
							console.error('获取文件失败', fileRes);
						}
					});
				} else {
					
					console.error('下载失败:', res);
				}
				wx.hideLoading();
			},
			fail: (err) => {
				console.log(err)
				wx.hideLoading();
				
			}
		})
	},
	onClick(event) {
		this.setData({ showShare: true });
	},

	onClose() {
		this.setData({ showShare: false });
	},

	onSelect(event) {
		this.onClose();
		if (event.detail.index == 1) {
			wx.showToast({
				title: '海报生成中',
				icon: 'loading',
				duration: 1000
			})
			this.setData({
				generated: true,
			})
			this.poster_fusion(); //生成小程序码
		}
	},
	// 播放视频
	playVideo_pust() {
		this.setData({
			'trends.itars_trfalse': true
		})
	},
	onLoad(options) {
		if(options.id != 'undefined'){
			this.setData({
				id:options.id
			},()=>{
				this.init_data();
			})
		}
	},
	

	// trends: {
	// 	img_url: '../images/logo.png',
	// 	tile: '上海蜜度科技股份有限公司1',
	// 	tile_two: '新浪舆情通：国央企舆情管理的“智能帮手”',
	// 	publish: '李嘉',
	// 	time: '2023-12-12',
	// 	ti_top: '移动互联网时代，加快了信息传播、扩散速度。公众关注的“聚光灯”下，一向沉稳而内敛的国央企在舆论方面的声誉和形象也备受关注。',
	// 	video_prodet: 'https://images.qidasky.com/static/video/v3.mp4',
	// 	img_bg: '../images/bar.png',
	// 	number: '204',
	// 	itars_trfalse: false,
	// },
	// // 海报数据
	// userInfo: {
	// 	avatarUrl: '/pages/images/logo.png', //logo
	// 	qrcode: '/pages/images/tym.png',//太阳码
	// 	lovideo: '/pages/images/bar.png', // 视频背景
	// 	playdata: '/pages/images/play.png', // 播放按钮
	// 	nickName: '上海蜜度科技股份有限公司', //标题
	// 	ntishi: '长按二维码进入小程序',
	// 	mobile: '蜜度校对通入选《2023年度上海市第一...蜜度校对通入选《2023年度上海市第一...蜜度校对通入选《2023年度上海市第一...', //内容
	// },
	init_data(){
		var url = 'index/new_detail'
		getApp().util.request({
			url: url,
			data:{
				id:this.data.id
			}
		}).then( (res) => {
			this.setData({
				trends:res.detail,
				'userInfo.avatarUrl':res.detail.img_url,
				'userInfo.lovideo':res.detail.img_bg,
				'userInfo.nickName':res.detail.tile,
				'userInfo.mobile':res.detail.tile_two,
			})	
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},
	// onShareTimeline() {
	// 	return {
	// 		title: this.data.trends.tile_two,
	// 		imageUrl: 'https://example.com/shareTimeline.jpg' // 转发到朋友圈的图片链接
	// 	};
	// },
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {
		return {
			title: this.data.trends.tile,
			imageUrl: '',
			path: 'pages/ynamic/index?id='+this.data.id,
		}
	}
})